/**
 * Created by Administrator on 2017/5/3.
 */
$(function(){
    var token = localStorage.getItem('youyuLoginToken');
    var datas,dataB,dataC,dataD,dataE,dataF,dataG,dataH,name,descriptions;
    descriptions =$('.infos').val();
    var isCheck1 = false,isCheck2 = false,isCheck3 = false,isCheck4 = false,isCheck5 = false,isCheck6 = false,isCheck7 = false,isCheck8 = false;
    /*名字验证*/
    var nameV = $('.nickName');
    nameV.blur(function(){
        name = nameV.val();
    });
    /*获取图片*/

    //var imgUpAll = imgUp[m].value;
    /*循环生成li*/

    function arrayA(ele,arr){
        if(arr instanceof Array){
            var list="";
            for(var i =0;i<arr.length;i++){
                list+='<li data-value='+arr[i]+' class="checks">'+arr[i]+'</li>'
            }
            ele.html(list);
        }
    }
    /*后期优化*/
    $('.listA').delegate('li','click',function(){
        isCheck1 = true;
        $('.checked').removeClass('checked').addClass('checks');
        if($(this).is('.checks')){
            $(this).removeClass('checks').addClass('checked');
        }
        datas = $(this).attr('data-value');

    });
    $('.listB').delegate('li','click',function(){
        isCheck2 = true;
        $('.checked').removeClass('checked').addClass('checks');
        if($(this).is('.checks')){
            $(this).removeClass('checks').addClass('checked');
        }
        dataB = $(this).attr('data-value');

    });
    $('.listC').delegate('li','click',function(){
        isCheck3 = true;
        $('.checked').removeClass('checked').addClass('checks');
        if($(this).is('.checks')){
            $(this).removeClass('checks').addClass('checked');
        }
        dataC = $(this).attr('data-value');

    });
    $('.listD').delegate('li','click',function(){
        isCheck4 = true;
        $('.checked').removeClass('checked').addClass('checks');
        if($(this).is('.checks')){
            $(this).removeClass('checks').addClass('checked');
        }
        dataD = $(this).attr('data-value');

    });
    $('.listE').delegate('li','click',function(){
        isCheck5 = true;
        $('.checked').removeClass('checked').addClass('checks');
        if($(this).is('.checks')){
            $(this).removeClass('checks').addClass('checked');
        }
        dataE = $(this).attr('data-value');

    });
    $('.listF').delegate('li','click',function(){
        isCheck6 = true;
        $('.checked').removeClass('checked').addClass('checks');
        if($(this).is('.checks')){
            $(this).removeClass('checks').addClass('checked');
        }
        dataF = $(this).attr('data-value');

    });
    $('.listG').delegate('li','click',function(){
        isCheck7 = true;
        $('.checked').removeClass('checked').addClass('checks');
        if($(this).is('.checks')){
            $(this).removeClass('checks').addClass('checked');
        }
        dataG = $(this).attr('data-value');

    });
    $('.listH').delegate('li','click',function(){
        isCheck8 = true;
        $('.checked').removeClass('checked').addClass('checks');
        if($(this).is('.checks')){
            $(this).removeClass('checks').addClass('checked');
        }
        dataH = $(this).attr('data-value');

    });

    /*年龄*/
    var isClick = false;
    var arrayEge = new  Array('18','19','20','21','22','23','24','25','26','27','28','29','30','31','32','33','34','35','36','37','38','39','40','41','42','43','44','45','46','47','48','49','50')
    var egeDom = $('#lists');
    //身高数组
    var arrayHeight = new  Array('145','146','147','148','149','150','151','152','153','154','155','156','157','158','159','160','161','162','163','164','165','166','167','168','169','170','171','172','173','174','175','176','177','178','179','180','181','182','183','184','185','186','187','188','189','190','191','192','193','194','195','196','197','198','199','200','201','202','203','204','205','206','207','208','209','210')
    var heightDom = $('#heiLists');
    //体重数组
    var arrayWeight = new  Array('35','36','37','38','39','40','41','42','43','44','45','46','47','48','49','50','51','52','53','54','55','56','57','58','59','60','61','62','63','64','65','66','67','68','69','70','71','72','73','74','75')
    var weightDom = $('#weiLists');
    //星座数组
    var arrayConstel = new  Array('白羊座','金牛座','双子座','巨蟹座','狮子座','处女座','天秤座','天蝎座','射手座','摩羯座','水瓶座','双鱼座')
    var constelDom = $('#constelLists');
    //亲密行为数组
    var arrayinti = new  Array('接受','不接受');
    var intiDom = $('#intiLists');
    //现实交友数组
    var arrayFri = new  Array('接受','不接受');
    var friDom = $('#friLists');
    //喜爱类型数组
    var arrayLove = new  Array('妖艳妩媚','闭月羞花','国色天香','亭亭玉立','小家碧玉','倾国倾城','明眸皓齿','冰肌玉骨','环肥燕瘦','梨花带雨','窈窕淑女');
    var loveDon = $('#loveLists');
    //交友目的数组
    var arrayObjective = new  Array('长期交往，不再孤单','一夜就好，互不影响');
    var objectiveDom = $('#objectiveLists');
    $("#baseInfo").delegate('li','click',function(){
        if($(this).is('.age')){
            arrayA(egeDom,arrayEge);
            $('#tapNum').show();
            $('.finish').click(function(){
                if(isCheck1== true) {
                    $('#tapNum').hide();
                    $('.ages').text(datas + '岁');
                }
            })
        }else if($(this).is('.stature')){
            arrayA(heightDom,arrayHeight);
            $('#heiNum').show();
            $('.finish').click(function(){
                if(isCheck2 == true) {
                    $('.height').text(dataB + 'cm');
                    $('#heiNum').hide();
                }
            })
        }else if($(this).is('.weight')){
            arrayA(weightDom,arrayWeight);
            $('#weiNum').show();
            $('.finish').click(function(){
                if(isCheck3 == true) {
                    $('.weiN').text(dataC + 'kg');
                    $('#weiNum').hide();
                }
            })
        }else  if($(this).is('.constellation')){
            arrayA(constelDom,arrayConstel);
            $('#constelNum').show();
            $('.finish').click(function(){
                if(isCheck4 == true) {
                    $('.constel').text(dataD);
                    $('#constelNum').hide();
                }
            })
        }
    });
    $("#detailInfo").delegate('li','click',function(){
        if($(this).is('.intimacy')){
            arrayA(intiDom,arrayinti);
            $('#intiNum').show().css('height','4rem');
            $('.finish').click(function(){
                if(isCheck5 == true){
                    $('#intiNum').hide();
                    $('.intima').text(dataE);
                }
            })
        }else  if($(this).is('.friend')){
            arrayA(friDom,arrayFri);
            $('#friendNum').show().css('height','4rem');
            $('.finish').click(function(){
                if(isCheck6 == true) {
                    $('#friendNum').hide();
                    $('.friA').text(dataF);
                }
            })
        }else if($(this).is('.loveinte')){
            arrayA(loveDon,arrayLove);
            $('#loveNum').show();
            $('.finish').click(function(){
                if(isCheck7 == true) {
                    $('#loveNum').hide();
                    $('.love').text(dataG);
                }
            })
        }else if($(this).is('.objective')){
            arrayA(objectiveDom,arrayObjective);
            $('#objectNum').show().css('height','4rem');
            $('.finish').click(function(){
                if(isCheck8 == true) {
                    $('#objectNum').hide();
                    $('.objectT').text(dataH);
                }
            })
        }
    })
    $(".calc").click(function(){
        $('#tapNum').hide();
        $('#heiNum').hide();
        $('#weiNum').hide();
        $('#constelNum').hide();
        $('#intiNum').hide();
        $('#friendNum').hide();
        $('#loveNum').hide();
        $('#objectNum').hide();
    });
    /*提交*/
    $('.save').click(function(){

        descriptions =$('.infos').val();
        var imgFile=[],imgFileString;
        var imgUp =$("input[name='uploadImg[]']");

        for(var m in imgUp){
            imgFile.push(imgUp[m].value);
        }
        imgFileString = imgFile.join(",");
        if(dataE == "接受"){
            dataE = 1;
        }else {
            dataE =2;
        }
        if(dataF == "接受"){
            dataF = 1;
        }else {
            dataF =2;
        }
            var packUrl = UPDATAUSER ;
            var curTime = new Date().getTime();
            var packParam ={'photowall':imgFileString,'nickname':name,'age':datas,'height':dataB,'weight':dataC,'constellation':dataD,'signature':descriptions,'acceptIntimateBehavior':dataE,'factFriend':dataF,'likeType':dataG,'purpose':dataH};
            var userInfo = {
                token:token,
                time: curTime,
                url:packUrl,
                param:packParam,
                sign:$.md5('ts='+curTime+'&mn=3&chid=0&version=1.0&token='+token+'&param='+JSON.stringify(packParam)),
                okCallback:function(res){
                    if(res.result == 200){
                        //更新本地youyuUserData
                        var userData = localStorage.getItem('youyuUserData');
                        var uid = $.parseJSON(userData).uid;

                        var inforUrl =LOAD_INFO;
                        var curTime = new Date().getTime();
                        var packParam ={'uid':uid};
                        var getUserInforimation = {
                            token: token,
                            time: curTime,
                            url: inforUrl,
                            param: packParam,
                            sign: $.md5('ts=' + curTime + '&mn=3&chid=0&version=1.0&token=' + token + '&param=' + JSON.stringify(packParam)),
                            okCallback: function (res) {
                                localStorage.setItem('youyuUserData',JSON.stringify(res.data));
                            },
                            errorCallback: function () {

                            }
                        }

                        YY.post(getUserInforimation);

                    }
                    setCookie('signature',descriptions,7200);
                    window.location.href='personal.html';
                },
                errorCallback:function(){

                }
            }
            YY.post(userInfo);

    })
    /*个人信息展示*/
    var userData = localStorage.getItem('youyuUserData');
    var userFrom = JSON.parse(userData);

    if(userFrom.acceptIntimateBehavior == 1){
        userFrom.acceptIntimateBehavior = '接受';
    }else if(userFrom.acceptIntimateBehavior == 2){
        userFrom.acceptIntimateBehavior = '不接受';
    }
    if(userFrom.factFriend == 1){
        userFrom.factFriend = '接受';
    }else if(userFrom.factFriend == 2){
        userFrom.factFriend = '不接受';
    }
    nameV.val(userFrom.nickname ? userFrom.nickname :"");
    $('.ages').text(userFrom.age ? userFrom.age + '岁' :"");
    $('.height').text(userFrom.height ? userFrom.height+ 'cm' : "");
    $('.weiN').text(userFrom.weight ? userFrom.weight + 'kg' : "");
    $('.constel').text(userFrom.constellation ? userFrom.constellation :"");
    $('.intima').text(userFrom.acceptIntimateBehavior ? userFrom.acceptIntimateBehavior : "");
    $('.friA').text(userFrom.factFriend ? userFrom.factFriend : "" );
    $('.love').text(userFrom.likeType ? userFrom.likeType : "");
    $('.objectT').text(userFrom.purpose ? userFrom.purpose : "");
    $('.infos').val(getCookie('signature'));
    var userPhoto = $.grep(userFrom.photowall.split(','), function(n) {return $.trim(n).length > 0;})

    var imgUp ='';
    for(var n in userPhoto){
        var photoHtml = '<div class="file-item"><div class="star-imagepath star-dash star-left star-hide" style="display: block;"><div class="star-imagepreview" data-id="o_1bfqjto8su0eoq4177t6at5l29"><span class="remove-img" title="点击删除当前项"></span><input type="hidden" name="uploadImg[]" value="'+userPhoto[n]+'" class="fileImg"><input type="hidden" name="uploadImgFrame[]" value="'+userPhoto[n]+'"><img src="'+userPhoto[n]+'"></div><div class="star-clear"></div></div></div>';
        $('.z_photo .file-item:last-child').before(photoHtml);
    }

})
/*cookie个人信息*/
function setCookie(name, value, iDay){
    var oDate=new Date();
    oDate.setDate(oDate.getDate()+iDay);
    document.cookie=name+'='+value+';expires='+oDate;
}
function getCookie(name){
    var arr=document.cookie.split('; ');
    for(var i=0;i<arr.length;i++){
        var arr2=arr[i].split('=');
        if(arr2[0]==name){
            return arr2[1];
        }
    }
    return '';
}
function removeCookie(name){
    setCookie(name, 1, -1);
}

